﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

using Microsoft.Xna.Framework;

namespace Alex_s_Dream_The_Toy_World
{
    public enum TerrainPoint
    {
        MOUNTAIN = 0,
        GROUND,
        WATER,
    }

    public class Map
    {
        private int brickSize;

        public int BrickSize
        {
            get { return brickSize; }
            set { brickSize = value; }
        }

        private int size;

        public int Size
        {
            get { return size; }
            set 
            { 
                size = value;
                pointList = new TerrainPoint[size, size];
            }
        }

        private TerrainPoint[,] pointList;

        public TerrainPoint[,] PointList
        {
            get { return pointList; }
            set 
            {
                for (int i = 0; i < Size; i++)
                    for (int j = 0; j < Size; j++)
                    {
                        // Copy
                        pointList[i, j] = value[i, j];
                    }
            }
        }

        private FrameworkCore game;

        public FrameworkCore Game
        {
            get { return game; }
            set { game = value; }
        }

        private float terrainMinHeight;

        public float TerrainMinHeight
        {
            get { return terrainMinHeight; }
            set { terrainMinHeight = value; }
        }

        private float terrainMaxHeight;

        public float TerrainMaxHeight
        {
            get { return terrainMaxHeight; }
            set { terrainMaxHeight = value; }
        }

        public Map(FrameworkCore game)
        {
            Game = game;
            Size = 0;
            BrickSize = 0;
            pointList = new TerrainPoint[0,0];
        }

        public Vector3 ConvertMapPositionToNomalPosition(System.Drawing.Point position)
        {
            // Get Terrain Entity
            MsgGetTerrainEntity getTerrainMsg = ObjectPool.Aquire<MsgGetTerrainEntity>();
            getTerrainMsg.UniqueTarget = this.Game.TerrainID;
            this.Game.SendMessage(getTerrainMsg);

            Terrain terrain;
            terrain = getTerrainMsg.TerrainEntity;

            List<System.Drawing.Point> brickList = new List<System.Drawing.Point>();
            for (int i = 0; i < BrickSize; i++)
                for (int j = 0; j < BrickSize; j++)
                {
                    brickList.Add(new System.Drawing.Point(position.X * BrickSize + i, position.Y * BrickSize + j));
                }
            float averageHeight = terrain.GetAverageHeight(brickList);

            if (averageHeight < 0)
            {
                throw new Exception("Do cao am");
            }

            return new Vector3((position.X * BrickSize + (float)BrickSize / 2) * terrain.ScaleFactor, averageHeight, (position.Y * BrickSize + (float)BrickSize / 2) * terrain.ScaleFactor);
        }

        public System.Drawing.Point ConvertNomalPositionToMapPosition(Vector3 position)
        {
            // Get Terrain Entity
            MsgGetTerrainEntity getTerrainMsg = ObjectPool.Aquire<MsgGetTerrainEntity>();
            getTerrainMsg.UniqueTarget = this.Game.TerrainID;  
            this.Game.SendMessage(getTerrainMsg);

            Terrain terrain;
            terrain = getTerrainMsg.TerrainEntity;

            System.Drawing.Point terrainPosition = terrain.ConvertNomalPositionToTerrainPosition(position);
            System.Drawing.Point mapPosition = new System.Drawing.Point(terrainPosition.X / this.BrickSize, terrainPosition.Y / this.BrickSize);
            return mapPosition;

        }
    }
}
